package com.gpjcjl.ui;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.base.lib.Logs;
import com.gpjcjl.App;
import com.gpjcjl.database.DbHelper;
import com.gpjcjl.entity.Record;
import com.gpjcjl.entity.Result;
import com.gpjcjl.event.HisRecordUpdate;
import com.gpjcjl.event.RecordUpdate;
import com.gpjcjl.http.HttpManager;
import com.gpjcjl.http.HttpService;
import com.gpjcjl.http.RxSchedulers;
import com.gpjcjl.http.SingleLiveEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author yinbiao
 */
public class RecordUpdateViewModel extends AndroidViewModel {

    private static final String TAG = "CreateViewModel";

    CompositeDisposable disposable;

    public RecordUpdateViewModel(@NonNull Application application) {
        super(application);
        disposable = new CompositeDisposable();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        //页面销毁时调用
        Logs.d(TAG, "onCleared:");
        disposable.clear();
    }

    /**
     * 修改记录
     *
     * @return LiveData
     */
    @SuppressLint("HardwareIds")
    public LiveData<Result<String>> updateRecord(boolean isRemote, Record record) {
        MutableLiveData<Result<String>> result = new SingleLiveEvent<>();
        try {
            if (isRemote) {
                Map<String, String> params = new HashMap<>();
                params.put("report_num", record.getReport_num());
                params.put("rid", record.getId());
                //                params.put("water", record.getWater());
                //                params.put("ambient", record.getAmbient());
                //                params.put("check_time", record.getCheck_time());
                params.put("property", record.getProperty());
                params.put("specs", record.getSpecs());
                disposable.add(HttpManager.getInstance().getService(HttpService.class)
                        .editRecord(App.getInstance().getAndroidId(), params)
                        .compose(RxSchedulers.ioMain())
                        .subscribe(bean -> {
                            if (bean.isSuccess()) {
                                result.setValue(Result.success(bean.getMsg()));
                                EventBus.getDefault().post(new HisRecordUpdate());
                            } else {
                                result.setValue(Result.error(bean.getMsg()));
                            }
                        }, throwable -> {
                            result.setValue(Result.error(throwable.getMessage()));
                            Logs.e(TAG, throwable.toString() + "-----" + throwable.getMessage());
                        }));
            } else {
                DbHelper.getInstance().getDataBase().recordDao().update(record);
                EventBus.getDefault().post(new RecordUpdate());
                result.setValue(Result.success("保存成功"));
            }
        } catch (Exception e) {
            result.setValue(Result.error("异常：" + e.getMessage()));
        }
        return result;
    }

}
